let app = getApp();
var QQMapWX = require("../../../utils/qqmap-wx-jssdk.js"),
  qqmapsdk, that
var that;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    banner: [],
    cika: [],
    category: [],
    coupons: [],
    sqlq: !1,
    privilegeIndexList: [],
    enjoyIndexList: [],
    shopData: {},
    inforList: [],
    vipLevelInfo: {},
    bizUserInfo: {},
    page: 1,
    rows: 6,
    special: []
  },
  sqlq: function() {
    console.log("sqlq");
    this.setData({
      sqlq: !this.data.sqlq
    });
  },
  // 立即开卡
  ljkk: function() {
    var t = this.data.userInfo;
    "" != t.img && "" != t.nickname ? (console.log(this.data.xtxx.is_stk), "1" == this.data.xtxx.is_stk ? wx.showModal({
      title: "提示",
      content: "请选择开卡类型",
      confirmText: "办会员卡",
      cancelColor: "#3CC51F",
      cancelText: "绑定实卡",
      success: function(t) {
        t.confirm ? (console.log("用户点击确定"), wx.navigateTo({
          url: "../../index/pages/cardOpen"
        })) : t.cancel && (console.log("用户点击取消"), wx.navigateTo({
          url: "../../index/pages/stklogin"
        }));
      }
    }) : wx.navigateTo({
      url: "../my/login"
    })) : wx.navigateTo({
      url: "/zh_vip/pages/index/getdl"
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this
    qqmapsdk = new QQMapWX({
      key: 'SDSBZ-OV63U-Q2LVD-2RQWX-G2FE3-W3F7F'
    });
    wx.getSetting({
      success: function(res) {
        console.log(res)
        if (!res.authSetting['scope.userInfo']) {
          wx.navigateTo({
            url: '/page/index/pages/toAuth/toAuth'
          })
        }
      },
      fail: function(res) {
        console.log(res)
      }
    })
    if (wx.getStorageSync('bizUserInfo')) {
      that.setData({
        bizUserInfo: wx.getStorageSync('bizUserInfo')
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    //专题
    that.special()
    //首页banner
    that.getIndexBanner()
    // // 获取门店信息
    // that.getShopInfo()
    //首页优惠券
    that.getCouponData()
    //公告
    that.getNotifyData()
    //会员特权
    that.getVipPri()
    //会员尊享
    that.getVipEnjoy()
    // 次卡
    that.getCardData()

    // 调用地图
    wx.getLocation({
      type: 'wgs84',
      success: function(res) {
        console.log(res)
        app.globalData.locationInfo = res
        if (!app.globalData.shopData) {
          that.getShopInfo()
        } else {
          that.setData({
            shopData: app.globalData.shopData
          })
        }
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: app.globalData.locationInfo.latitude,
            longitude: app.globalData.locationInfo.longitude
          },
          coord_type: 1,
          success: function(res) {
            wx.setStorageSync("ad_info", res.result.ad_info)
          },
          fail: function(res) {
            console.log(res);
          }
        })
      },
      fail: function(res) {
        console.log(res)
        wx.showModal({
          title: '提示',
          content: '授权定位失败，部分功能不能使用，点击确定重新授权定位',
          showCancel: false,
          success: function(res) {
            if (res.confirm) {
              if (wx.openSetting) {
                wx.openSetting({
                  success: function(res) {
                    if (res.authSetting["scope.userLocation"]) { //如果用户重新同意了授权登录
                      that.onLoad()
                    }
                  },
                  fail: function(res) {

                  }
                })
              } else {
                wx.showToast({
                  title: '微信版本低，请升级！',
                })
              }
            } else {
              console.log('授权定位失败')
            }
          },
          fail: function(res) {

          }
        })
      }
    })

    //获取业务用户信息
    that.getBizUserInfo()
    //获取当前会员等级信息
    that.getVipLevelData()
    if (wx.getStorageSync('bizUserInfo') !== null) {
      console.log(wx.getStorageSync('bizUserInfo'))
      that.setData({
        bizUserInfo: wx.getStorageSync('bizUserInfo')
      })
        wx.request({ // 分销 扫码注册
          url: app.api.scanCode(),
          method: 'POST',
          data: {
            openid: wx.getStorageSync('bizUserInfo').openid,
            wxHeadimg: wx.getStorageSync('bizUserInfo').wxHeadimg,
            wxNickName: wx.getStorageSync('bizUserInfo').wxNickName,
            wxNickNameStr: wx.getStorageSync('bizUserInfo').wxNickNameStr,
            pid: Number(wx.getStorageSync('sceneID'))
          },
          success: function(res) {
            console.log(res)
          }
        })
      }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**
   * 获取用户信息授权
   */
  userInfo: function() {
    wx.navigateTo({
      url: '../../index/pages/cardOpen'
    })
  },
  /**
   * 首页获取最近的门店
   */
  getShopInfo: function(e) {
    if (app.globalData.locationInfo) {
      app.getData({
        url: app.api.getShopInfo(),
        method: 'POST',
        data: {
          latitude: app.globalData.locationInfo.latitude,
          longitude: app.globalData.locationInfo.longitude
        },
        ok: function(res) {
          console.log(res)
          if (res.statusCode == 200 && res.errMsg == 'request:ok') {
            if (res.data.code == 1) {
              app.globalData.shopData = res.data.data.list["0"]
              app.globalData.companyId = res.data.data.list["0"].companyId
              wx.setStorageSync("shopData", res.data.data.list["0"])
              wx.setStorageSync('companyId', res.data.data.list["0"].companyId)
              that.setData({
                shopData: res.data.data.list["0"]
              })
              console.log(that.data.shopData)
              //会员特权
              that.getVipPri()
              //会员尊享
              that.getVipEnjoy()
              // 次卡
              that.getCardData()
              //首页优惠券
              that.getCouponData()
            }
          }
        }
      })
    }
  },
  /**
   * 跳转地图定位
   */
  toLocation: function(t) {
    console.log(t.currentTarget.dataset.location);
    var a = t.currentTarget.dataset.location.split(","),
      e = t.currentTarget.dataset.name,
      b = t.currentTarget.dataset.address;
    console.log(a);
    wx.openLocation({
      latitude: Number(a[1]),
      longitude: Number(a[0]),
      name: e,
      address: b
    });

  },
  // 会员尊享,会员权益
  getVipPri: function() {
    var priType = 1
    that.getVipData(priType)
  },
  getVipEnjoy: function() {
    var enjoyType = 2
    that.getVipData(enjoyType)
  },
  getVipData: function(res) {
    console.log(app.globalData.shopData)
    var vipType = res
    if (wx.getStorageSync('vipLevelInfo') == '' || wx.getStorageSync('vipLevelInfo') == null) {
      app.getData({
        url: app.api.getPrivilegeList(that.data.page, that.data.rows),
        method: 'POST',
        data: {
          enjoyType: vipType,
          companyId: wx.getStorageSync('companyId')
        },
        ok: function(res) {
          console.log(res)
          if (res.statusCode == 200 && res.errMsg == 'request:ok') {
            if (res.data.code == 1) {
              console.log(vipType)
              if (vipType == 1 || vipType == '1') {
                that.setData({
                  privilegeIndexList: res.data.data.list
                })
              } else {
                that.setData({
                  enjoyIndexList: res.data.data.list
                })
              }
            } else {
              wx.showToast({
                title: res.data.message,
              })
            }
          }
        }
      })
    } else {
      app.getData({
        url: app.api.getPrivilegeList(that.data.page, that.data.rows),
        method: 'POST',
        data: {
          level: wx.getStorageSync('vipLevelInfo').level,
          enjoyType: vipType,
          companyId: wx.getStorageSync('companyId')
        },
        ok: function(res) {
          console.log(res)
          if (res.statusCode == 200 && res.errMsg == 'request:ok') {
            if (res.data.code == 1) {
              console.log(vipType)
              if (vipType == 1 || vipType == '1') {
                that.setData({
                  privilegeIndexList: res.data.data.list
                })
              } else {
                that.setData({
                  enjoyIndexList: res.data.data.list
                })
              }
            } else {
              wx.showToast({
                title: res.data.message,
              })
            }
          }
        }
      })
    }
  },
  // 首页banner
  getIndexBanner: function(res) {
    var $this = this;
    app.getData({
      url: app.api.getIndexBanner() + `?shopId=${wx.getStorageSync('shopData').id}&page=${1}&rows=${3}&adCategory=${1}`,
      method: 'GET',
      ok: function(res) {
        console.log(res)
        if (res.statusCode == 200 && res.errMsg == 'request:ok') {
          if (res.data.code == 1) {
            console.log(res.data.list)
            $this.setData({
              banner: res.data.list,
            });
          } else {
            wx.showToast({
              title: res.data.message,
            })
          }
        }
      }
    })
  },
  // 优惠券
  getCouponData: function() {
    var that = this,
      page = 1,
      limit = 3,
      numberId = wx.getStorageSync('bizUserInfo').id
    app.getData({
      url: app.api.getCoupons(page, limit),
      method: 'POST',
      data: {
        memberId: numberId,
        companyId: app.globalData.companyId
      },
      ok: function(res) {
        console.log(res)
        if (res.statusCode == 200 && res.errMsg == 'request:ok') {
          if (res.data.code == 1) {
            that.setData({
              coupons: res.data.list,
            });
            // this.tab_config = res.data.data
            console.log(that.data.coupons)

          } else {
            wx.showToast({
              title: res.data.message,
            })
          }
        }
      }
    })
  },
  //次卡
  getCardData: function(res) {
    app.getData({
      url: app.api.getCika(),
      method: 'GET',
      data: {
        companyId: app.globalData.companyId,
        page: 1,
        rows: 3
      },
      ok: function(res) {
        console.log(res)
        if (res.statusCode == 200 && res.errMsg == 'request:ok') {
          if (res.data.code == 1) {
            console.log(res.data.list)
            that.setData({
              cika: res.data.list,

            });


          } else {
            wx.showToast({
              title: res.data.message,
            })
          }
        }
      }
    })
  },
  // 立即领取优惠券
  onRceive: function(e) {
    console.log(e)
    if (app.isLogin()) {
      //写跳转，或者其他
      app.getData({
        url: app.api.getRceive(),
        method: 'GET',
        data: {
          couponId: e.currentTarget.dataset.id,
          memberId: that.data.bizUserInfo.id
        },
        ok: function(res) {
          console.log(res)
          if (res.statusCode == 200 && res.errMsg == 'request:ok') {
            if (res.data.code == 1) {
              //首页优惠券
              that.getCouponData()
            } else {
              wx.showToast({
                title: res.data.message,
              })
            }
          }
        }

      })
    }

  },
  //打电话
  callPhone: function(res) {
    console.log(res)
    wx.makePhoneCall({
      phoneNumber: res.currentTarget.dataset.phone
    })
  },
  getNotifyData() {
    app.getData({
      url: app.api.getNotifyData(1, 3),
      ok: function(res) {
        if (res.statusCode == 200 && res.errMsg == 'request:ok') {
          if (res.data.code == 1) {
            that.setData({
              inforList: res.data.list
            })
          } else {
            console.log('首页公告信息错误')
          }
        } else {
          console.log('首页公告信息获取失败')
        }
      }
    })
  },
  getBizUserInfo() {
    app.getData({
      url: app.api.getBizUserInfo(),
      data: {
        openid: wx.getStorageSync('openId')
      },
      ok: function(res) {
        if (res.statusCode == 200 && res.errMsg == 'request:ok') {
          if (res.data.code == 1) {
            if (res.data.data!=null) {
              app.globalData.memberLevelId = res.data.data.memberLevelId,
                wx.setStorageSync('bizUserInfo', res.data.data)
            }

            //首页优惠券
            that.getCouponData()
          }
        }
      }
    })
  },
  getVipLevelData() {
    app.getData({
      url: app.api.getVipLevelData(),
      data: {
        openid: wx.getStorageSync('openId')
      },
      ok: function(res) {
        console.log(res)
        if (res.statusCode == 200 && res.errMsg == 'request:ok') {
          if (res.data.code == 1) {
            // app.globalData.memberLevelId = res.data.data.memberLevelId,
            //   wx.setStorageSync('bizUserInfo', res.data.data)
            that.setData({
              vipLevelInfo: res.data.data
            })
            wx.setStorageSync('vipLevelInfo', res.data.data)
          }
        }
      }
    })
  },
  itemClick: function(res) {
    console.log(res)
    wx.navigateTo({
      url: res.currentTarget.dataset.item.enjoyLink,
    })
  },
  toCardDetail: function(res) {
    console.log(res)
    wx.navigateTo({
      url: '../../index/pages/cardCenter/cardCenterDetail/cardCenterDetail?id=' + res.currentTarget.dataset.item
    })
  },
  special: function() {
    app.getData({
      url: app.api.getSpecial(),
      method: 'POST',
      data: {
        companyId: wx.getStorageSync("companyId"),
        limit: 3,
        page: 1,
        memberId: wx.getStorageSync('bizUserInfo').id,
        // specialChoice: 1
      },
      ok: function(res) {
        console.log(res)
        if (res.statusCode == 200 && res.errMsg == 'request:ok') {
          if (res.data.code == 1) {
            that.setData({
              special: res.data.list
            })
          }
        }
      }
    })
  },
})